<template>
  <div class="history-records-add">
    <el-card shadow="never" class="fluid-card">
      <div class="table-title">
        <span>种母历史记录批量新增</span>
        <el-button type="primary" @click="addRow" size="small">+ 增行</el-button>
      </div>
      <el-table :data="formList" border style="width: 100%;" height="calc(60vh - 120px)" :summary-method="getSummaries"
        show-summary :row-class-name="rowClassName">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="SheepFilesId" label="种母耳号Id" width="120">
          <template #default="scope">
            <el-input v-model="scope.row.SheepFilesId" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="MatingDate" label="配种日期" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.MatingDate" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="SheepFileId" label="种公耳号Id" width="120">
          <template #default="scope">
            <el-input v-model="scope.row.SheepFileId" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="PregnancyDate" label="妊娠日期" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.PregnancyDate" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="PregnancyResult" label="妊娠结果" width="100">
          <template #default="scope">
            <el-input v-model="scope.row.PregnancyResult" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="LambingDate" label="分娩日期" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.LambingDate" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="HealthyLambCount" label="健羔数" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.HealthyLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="WeakLambCount" label="弱羔数" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.WeakLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="DeadLambCount" label="死羔数" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.DeadLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="DeformedLambCount" label="畸形羔数" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.DeformedLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="StillbirthCount" label="死胎数" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.StillbirthCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="MummifiedLambCount" label="木乃伊羔数" width="100">
          <template #default="scope">
            <el-input-number v-model="scope.row.MummifiedLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="WeaningDate" label="断奶日期" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.WeaningDate" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="WeaningLambCount" label="断奶只数" width="100">
          <template #default="scope">
            <el-input-number v-model="scope.row.WeaningLambCount" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="WeaningTotalWeight" label="断奶总重量" width="120">
          <template #default="scope">
            <el-input-number v-model="scope.row.WeaningTotalWeight" size="small" :min="0" :step="0.01" />
          </template>
        </el-table-column>
        <el-table-column prop="WeaningAvgWeight" label="断奶平均重量" width="120">
          <template #default="scope">
            <el-input-number v-model="scope.row.WeaningAvgWeight" size="small" :min="0" :step="0.01" />
          </template>
        </el-table-column>
        <el-table-column prop="BuildingId" label="栋舍Id" width="80">
          <template #default="scope">
            <el-input v-model="scope.row.BuildingId" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="Field" label="栏位Id" width="80">
          <template #default="scope">
            <el-input v-model="scope.row.Field" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="Parity" label="当前胎次" width="80">
          <template #default="scope">
            <el-input-number v-model="scope.row.Parity" size="small" :min="0" />
          </template>
        </el-table-column>
        <el-table-column prop="Creator" label="创建人" width="100">
          <template #default="scope">
            <el-input v-model="scope.row.Creator" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="ResponsiblePerson" label="负责人" width="100">
          <template #default="scope">
            <el-input v-model="scope.row.ResponsiblePerson" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="CreateTime" label="创建时间" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.CreateTime" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="Modifier" label="修改人" width="100">
          <template #default="scope">
            <el-input v-model="scope.row.Modifier" size="small" />
          </template>
        </el-table-column>
        <el-table-column prop="ModifyTime" label="修改时间" width="120">
          <template #default="scope">
            <el-date-picker v-model="scope.row.ModifyTime" type="date" size="small" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="scope">
            <el-button type="danger" size="small" @click="removeRow(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-footer">
        <el-button type="primary" @click="submit" :loading="loading">提交</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { Sheep_BreedingRead, Sheep_BreedingWrite } from '@/utils/Interceptors'
const formList = ref([
  {
    SheepFilesId: '',
    MatingDate: '',
    SheepFileId: '',
    PregnancyDate: '',
    PregnancyResult: '',
    LambingDate: '',
    HealthyLambCount: 0,
    WeakLambCount: 0,
    DeadLambCount: 0,
    DeformedLambCount: 0,
    StillbirthCount: 0,
    MummifiedLambCount: 0,
    WeaningDate: '',
    WeaningLambCount: 0,
    WeaningTotalWeight: 0,
    WeaningAvgWeight: 0,
    BuildingId: '',
    Field: '',
    Parity: 0,
    Creator: '',
    ResponsiblePerson: '',
    CreateTime: '',
    Modifier: '',
    ModifyTime: ''
  }
])
const loading = ref(false)

function addRow() {
  formList.value.push({
    SheepFilesId: '',
    MatingDate: '',
    SheepFileId: '',
    PregnancyDate: '',
    PregnancyResult: '',
    LambingDate: '',
    HealthyLambCount: 0,
    WeakLambCount: 0,
    DeadLambCount: 0,
    DeformedLambCount: 0,
    StillbirthCount: 0,
    MummifiedLambCount: 0,
    WeaningDate: '',
    WeaningLambCount: 0,
    WeaningTotalWeight: 0,
    WeaningAvgWeight: 0,
    BuildingId: '',
    Field: '',
    Parity: 0,
    Creator: '',
    ResponsiblePerson: '',
    CreateTime: '',
    Modifier: '',
    ModifyTime: ''
  })
}
function removeRow(index) {
  formList.value.splice(index, 1)
}
function getSummaries({ columns, data }) {
  const sums = ['合计']
  const sumFields = [
    'HealthyLambCount', 'WeakLambCount', 'DeadLambCount', 'DeformedLambCount',
    'StillbirthCount', 'MummifiedLambCount', 'WeaningLambCount', 'WeaningTotalWeight', 'WeaningAvgWeight', 'Parity'
  ]
  columns.forEach((column, index) => {
    if (index === 0) return
    const key = column.property
    if (sumFields.includes(key)) {
      const total = data.reduce((sum, row) => sum + Number(row[key] || 0), 0)
      sums[index] = total.toFixed(2)
    } else {
      sums[index] = ''
    }
  })
  return sums
}
function rowClassName({ rowIndex }) {
  return rowIndex % 2 === 1 ? 'table-row-odd' : ''
}
async function submit() {
  loading.value = true

  const res = await Sheep_BreedingWrite.post('/api/HistoryRecord/AddHistory', formList.value)
  console.log('API响应:', res.data) // 调试用
  if (res.data && res.data.code === 200) {
    ElMessage.success('批量新增成功')
    formList.value = [
      {
        SheepFilesId: '',
        MatingDate: '',
        SheepFileId: '',
        PregnancyDate: '',
        PregnancyResult: '',
        LambingDate: '',
        HealthyLambCount: 0,
        WeakLambCount: 0,
        DeadLambCount: 0,
        DeformedLambCount: 0,
        StillbirthCount: 0,
        MummifiedLambCount: 0,
        WeaningDate: '',
        WeaningLambCount: 0,
        WeaningTotalWeight: 0,
        WeaningAvgWeight: 0,
        BuildingId: '',
        Field: '',
        Parity: 0,
        Creator: '',
        ResponsiblePerson: '',
        CreateTime: '',
        Modifier: '',
        ModifyTime: ''
      }
    ]
  } else {
    ElMessage.error(res.data?.msg || '批量新增失败')
  }



}
</script>

<style scoped>
.history-records-add {
  width: 100%;
  height: 100%;
  padding: 0;
  background: #fff;
}

.fluid-card {
  width: 100%;
  height: 100%;
  min-height: 60vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.table-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-weight: bold;
}

.table-footer {
  margin-top: 16px;
  text-align: right;
}

.el-table {
  background: #fff;
}

.table-row-odd {
  background: #fafafa;
}
</style>
